package com.qf.sl.youshare.adapters;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;
import com.qf.sl.youshare.Constants;
import com.qf.sl.youshare.R;
import com.qf.sl.youshare.model.ProductCollect;
import com.qf.sl.youshare.utils.FileCache;
import com.qf.sl.youshare.utils.ImageCache;
import com.qf.sl.youshare.utils.ImageLoader;

import java.util.List;

/**
 * Created by IDEA
 * User : sl
 * on 2015/2/26.
 * 21:09
 */
public class ProductCollectAdapter extends BaseAdapter {

    private LayoutInflater inflater;
    private List<ProductCollect> productCollects;

    private View.OnClickListener clickListener;

    public void setClickListener(View.OnClickListener clickListener) {
        this.clickListener = clickListener;
    }

    public ProductCollectAdapter(Context context, List<ProductCollect> productCollects) {
        if (context != null) {
            inflater = LayoutInflater.from(context);
        }else {
            throw new IllegalArgumentException("context must not null");
        }
        this.productCollects = productCollects;
    }

    @Override
    public int getCount() {
        int ret = 0;
        if (productCollects != null) {
            ret = productCollects.size();
        }
        return ret;
    }

    @Override
    public Object getItem(int position) {
        Object ret = null;
        if (productCollects != null) {
            ret = productCollects.get(position);
        }
        return ret;
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        View ret = null;

        if (convertView != null) {
            ret = convertView;
        }else {
            ret = inflater.inflate(R.layout.item_product_collect,parent,false);
        }

        ViewHolder holder = (ViewHolder) ret.getTag();

        if (holder == null) {
            holder = new ViewHolder();

            holder.icoHolder = (ImageView) ret.findViewById(R.id.product_collect_ico);
            holder.titleHolder = (TextView) ret.findViewById(R.id.product_collect_title);
            holder.briefHolder = (TextView) ret.findViewById(R.id.product_collect_brief);
            holder.priceHolder = (TextView) ret.findViewById(R.id.product_collect_price);
            holder.deleteHolder = (TextView) ret.findViewById(R.id.product_collect_delete);

            ret.setTag(holder);
        }

        holder.deleteHolder.setTag(position);

        holder.deleteHolder.setOnClickListener(clickListener);

        ProductCollect productCollect = productCollects.get(position);

        String name = productCollect.getName();
        String brief = productCollect.getDescription();
        float price = productCollect.getPrice();

        holder.titleHolder.setText(name);
        holder.briefHolder.setText(brief);
        holder.priceHolder.setText("￥" + Float.toString(price) + "元");

        String thumb = productCollect.getThumb();
        if (thumb != null && thumb.length() > 0) {
            if (!thumb.startsWith("http://") && !thumb.startsWith("https://")){
                thumb = Constants.IMAGE_SERVER + thumb;
            }

            holder.icoHolder.setTag(thumb);

            ImageCache imageCache = ImageCache.getInstance();
            Bitmap image = imageCache.getImage(thumb);
            if (image != null) {
                holder.icoHolder.setImageBitmap(image);
            }else {
                FileCache fileCache = FileCache.getInstance();
                byte[] bytes = fileCache.getFileContent(thumb);
                if (bytes != null && bytes.length > 0) {
                    Bitmap bitmap = BitmapFactory.decodeByteArray(bytes, 0, bytes.length);
                    holder.icoHolder.setImageBitmap(bitmap);

                    imageCache.putImage(thumb,bitmap);
                }else {
                    ImageLoader task = new ImageLoader(holder.icoHolder);
                    task.execute(thumb);
                }
            }
        }
        notifyDataSetChanged();

        return ret;
    }

    private class ViewHolder{
        public ImageView icoHolder;
        public TextView titleHolder;
        public TextView briefHolder;
        public TextView priceHolder;
        public TextView deleteHolder;
    }
}
